<template>
  <div style="width:50%;text-align:center;margin:auto;">
    <el-form ref="dataForm" :rules="rules" :model="user" label-position="left" label-width="110px" style="margin-left:50px;margin-right:50px;">
      <el-form-item label="用户名" prop="userName">
        <el-input v-model.trim="user.userName" placeholder="请输入用户名" />
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model.trim="user.password" type="password" placeholder="请输入密码" @keyup.enter="createData" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="$emit('dialogFormVisibleEmit', false)">
        取消
      </el-button>
      <el-button type="primary" :loading="loading" @click="createData">
        确认
      </el-button>
    </div>
  </div>
</template>

<script>
import { validateUsername, validatePassword } from '@/utils/validator'
export default {
  name: 'DataForm',
  props: {
    loading: { type: Boolean, default: false }
  },
  data() {
    return {
      rules: {
        userName: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }]
      },
      user: {
        userName: '',
        password: ''
      }
    }
  },
  methods: {
    createData() {
      // this.temp2 = `userName=${this.temp.userName}&password=${this.temp.password}`
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$emit('createDataEmit', this.user, true)
        }
      })
    }
  }
}
</script>
